<?php
namespace Home\Controller;
use Think\Controller;
class JixiaosuanfashezhiController extends Controller {
    
    public function _initialize()
    {
        header("Access-Control-Allow-Origin: *");  

        header("Access-Control-Allow-Credentials: true"); 
      
        header("Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS"); //允许的请求类型
        
        header("Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept"); 

    }

    public function get_list()
    {
        $page = I("get.page");
        $limit = I("get.limit");
        $st_date = I("post.st_date");

        $where = "1 = 1 ";

        $sql = 'select s.*,a.dept_type,a.st_date from comm.sys_dept_info s left join performance.set_accountdepttype a on  a.dept_code = s.dept_code  ';
        
        if(!empty($st_date))
        {
            $st_date = strtotime($st_date);
            $sql .= " and a.st_date = '$st_date'";
        }
        else
        {
            die();
        }
        
        
        // if(!empty($cost_item_code))
        // {
        //     $sql .= " and c.cost_item_code = '$cost_item_code'";
        // }
        $M = M("");
        $data = $M->query($sql);
        $list = M('performance.set_accounttype')->select();
        $arr = [];
        for ($i=0; $i < count($data); $i++) 
        {
            if($data[$i]['attr'] == 1 && $data[$i]['account_date'] == $st_date)
            {
                $arr[] = $data[$i];
            }
        }
        $data = $arr;
        
        for ($i = 0; $i < count($list); $i++) 
        { 
            $list[$i]['checked'] = false;
        }



        for ($i=0; $i < count($data); $i++) 
        {
            for ($j=0; $j < count($list); $j++) 
            { 
               $data[$i]['accounttype'][] = $list[$j];
            }
        }

        for ($i=0; $i < count($data); $i++) 
        {
            for ($j=0; $j < count($data[$i]['accounttype']); $j++) 
            { 
               if($data[$i]['dept_type'] == $data[$i]['accounttype'][$j]['type_code'])
               {
                    $data[$i]['accounttype'][$j]['checked'] = true;
               }
            }
        }

        $json["data"] = $data;
        $json["code"] = 0;
        $json["count"] = M("performance.set_accountdepttype")->where($where)->count();
        $json["msg"] = "";
        
        $this->ajaxReturn($json,"json");
    }

    public function get_one()
    {
        $st_date = I("post.st_date");
        
        $info = M("performance.set_accountdepttype")->where("uuid = '$uuid'")->find();
        $info["dept_date"] = date("Y-m-d H:i:s",$info["dept_date"]);
        $json["data"]   = $info;
        $json["code"]   = 0;
        $json["msg"]    = "";

        $this->ajaxReturn($json,"json");
    }

    public function save_one()
    {
        $data = I("post.");
        $uuid = $data["uuid"];
        unset($data["uuid"]);

        $result = M("performance.set_accountdepttype")->where("uuid = '$uuid'")->save($data);
        if($result !== false)
        {
            $json["code"] = 0;
            $json["msg"] = "修改成功";
        }
        else
        {
            $json["code"] = -1;
            $json["msg"] = "修改失败";
        }
 
        $this->ajaxReturn($json,"json");
    }

    public function add_one()
    {
        $data = I("post.");
        $uuid = uuid();
        
        $data["dept_date"] = time();
        $result = M("performance.set_accountdepttype")->add($data);
        if($result !== false)
        {
            $json["code"] = 0;
            $json["msg"] = "添加成功";
        }
        else
        {
            $json["code"] = -1;
            $json["msg"] = "添加失败";
        }
 
        $this->ajaxReturn($json,"json");
    }

    public function get_accounttype()
    {
        $data = M("performance.set_accounttype")->select();
        for ($i=0; $i < count($data); $i++) 
        { 
            $info = $data[$i];
        }
        $json["data"] = $data;
        $json["code"] = 0;
        $json["count"] = M("performance.set_accounttype")->count();
        $json["msg"] = "";
        
        $this->ajaxReturn($json,"json");
    }

    public function set_field()
    {
        $dept_code  = I('post.dept_code');
        $type_code  = I('post.type_code');
        $st_date    = I('post.st_date');

        if(!empty($st_date))
        {
            $st_date = strtotime($st_date);
            //$sql .= " and a.st_date = '$st_date'";
        }
        else
        {
            die();
        }

        $where['dept_code'] = $dept_code;
        $where['st_date'] = $st_date;

        $data['dept_type'] = $type_code;

        $info = M("performance.set_accountdepttype")->where($where)->find();
        if($info)
        {
            
            $result = M("performance.set_accountdepttype")->where($where)->save($data);
        }
        else
        {
            $data['dept_code'] = $dept_code;
            $data['st_date'] = $st_date;
            $result = M("performance.set_accountdepttype")->add($data);
        }

        if($result !== false)
        {
            $json["code"] = 0;
            $json["msg"] = "修改成功";
        }
        else
        {
            $json["code"] = -1;
            $json["msg"] = "修改失败";
        }
        $this->ajaxReturn($json,"json");

    }

}